package co.fitstart.coach.entity.rest;

import java.math.BigDecimal;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.demo2do.core.utils.DateFormatUtils;
import com.demo2do.core.utils.DateUtils;

import co.fitstart.entity.site.Site;

/**
 * 
 * @author mohanlan
 *
 */
public class ArrangePrepare {
	
	private Long coach;
	
	private Date currentDay; 
	
	private boolean today;
	
	private String currentHour;
	
	private String startDate;
	
	/**
	 * 本周所有的休息时间总数
	 *   当前已选的休息时间总数，用于校验是否可以继续选择
	 */
	private int restCalendarSum;
	
	/** 
	 * 可选休息时间总数
	 *   由一周必须40小时上班时间与门店营业时间计算得出 
	 */
	private int restCalendarLimit;
	
	/** 可选的空闲时间 */
	private List<String> avaliableHours;
	
	/** 已选中的休息时间 */
	private List<String>  restCalendars;
	
	/** 已选中的体验课时间 */
	private List<String> experienceCalendars;
	
	/** 已选中的值班时间 */
	private List<String> dutyCalendars;
	
	public ArrangePrepare(Long coach, Date currentDay) {
		this.coach = coach;
		this.currentDay = currentDay;
	}
	
	/**
	 * 
	 * @return
	 */
	public ArrangePrepare init() {
		
		this.today = DateUtils.isSameDay(currentDay, new Date());
		this.currentHour = DateFormatUtils.format(new Date(), "HH:mm");
		this.startDate = DateFormatUtils.format(DateUtils.getFirstWeekDay(currentDay), "yyyy-MM-dd");
		
		return this;
	}
	
	/**
	 * 
	 * @return
	 */
	public ArrangePrepare initSelectLimit(Site site, int restCalendarSum) {
		
		this.restCalendarLimit = this.calculateLimit(site);
		
		this.restCalendarSum = restCalendarSum;
		
		return this;
	}
	
	/**
	 * 
	 * @param site
	 * @return
	 */
	private int calculateLimit(Site site) {
		
		BigDecimal restHour = new BigDecimal(site.getBusinessEnd()[0]).subtract(new BigDecimal(site.getBusinessStart()[0]));
        
        int compare = Integer.parseInt(site.getBusinessEnd()[1]) - Integer.parseInt(site.getBusinessStart()[1]);
        if(compare > 0) {
            
        	restHour = restHour.add(new BigDecimal("0.5"));
        } else if (compare < 0) {
        	
            restHour = restHour.subtract(new BigDecimal("0.5"));
        }
        
        restHour = restHour.multiply(new BigDecimal("7")).subtract(new BigDecimal("40")).multiply(new BigDecimal("2"));
        
        return restHour.intValue();
	}
	
	/**
	 * 
	 * @param restCalendars
	 * @param experienceCalendars
	 * @param dutyCalendars
	 * @param avaliableHours
	 * @return
	 */
	public ArrangePrepare initSelectItem(List<String> restCalendars, List<String> experienceCalendars, List<String> dutyCalendars, List<String> avaliableHours) {
		this.restCalendars = restCalendars;
		this.experienceCalendars = experienceCalendars;
		this.dutyCalendars = dutyCalendars;
		this.avaliableHours = avaliableHours;
		return this;
	}
	
	public Map<String, Object> describe() {
	    Map<String, Object> result = new HashMap<String, Object>();
	    
	    result.put("coach", this.coach);
	    result.put("currentDay", this.currentDay);
        result.put("today", this.today);
        result.put("currentHour", this.currentHour);
        result.put("startDate", this.startDate);
        result.put("restCalendarSum", this.restCalendarSum);
        result.put("restCalendarLimit", this.restCalendarLimit);
        result.put("avaliableHours", this.avaliableHours);
        result.put("restCalendars", this.restCalendars);
        result.put("experienceCalendars", this.experienceCalendars);
        result.put("dutyCalendars", this.dutyCalendars);
        
        return result;
	}

    /**
     * @return the coach
     */
    public Long getCoach() {
        return coach;
    }

    /**
     * @return the currentDay
     */
    public Date getCurrentDay() {
        return currentDay;
    }

    /**
     * @return the today
     */
    public boolean isToday() {
        return today;
    }

    /**
     * @return the currentHour
     */
    public String getCurrentHour() {
        return currentHour;
    }

    /**
     * @return the startDate
     */
    public String getStartDate() {
        return startDate;
    }

    /**
     * @return the restCalendarSum
     */
    public int getRestCalendarSum() {
        return restCalendarSum;
    }

    /**
     * @return the restCalendarLimit
     */
    public int getRestCalendarLimit() {
        return restCalendarLimit;
    }

    /**
     * @return the avaliableHours
     */
    public List<String> getAvaliableHours() {
        return avaliableHours;
    }

    /**
     * @return the restCalendars
     */
    public List<String> getRestCalendars() {
        return restCalendars;
    }

    /**
     * @return the experienceCalendars
     */
    public List<String> getExperienceCalendars() {
        return experienceCalendars;
    }

    /**
     * @return the dutyCalendars
     */
    public List<String> getDutyCalendars() {
        return dutyCalendars;
    }

    /**
     * @param coach the coach to set
     */
    public void setCoach(Long coach) {
        this.coach = coach;
    }

    /**
     * @param currentDay the currentDay to set
     */
    public void setCurrentDay(Date currentDay) {
        this.currentDay = currentDay;
    }

    /**
     * @param today the today to set
     */
    public void setToday(boolean today) {
        this.today = today;
    }

    /**
     * @param currentHour the currentHour to set
     */
    public void setCurrentHour(String currentHour) {
        this.currentHour = currentHour;
    }

    /**
     * @param startDate the startDate to set
     */
    public void setStartDate(String startDate) {
        this.startDate = startDate;
    }

    /**
     * @param restCalendarSum the restCalendarSum to set
     */
    public void setRestCalendarSum(int restCalendarSum) {
        this.restCalendarSum = restCalendarSum;
    }

    /**
     * @param restCalendarLimit the restCalendarLimit to set
     */
    public void setRestCalendarLimit(int restCalendarLimit) {
        this.restCalendarLimit = restCalendarLimit;
    }

    /**
     * @param avaliableHours the avaliableHours to set
     */
    public void setAvaliableHours(List<String> avaliableHours) {
        this.avaliableHours = avaliableHours;
    }

    /**
     * @param restCalendars the restCalendars to set
     */
    public void setRestCalendars(List<String> restCalendars) {
        this.restCalendars = restCalendars;
    }

    /**
     * @param experienceCalendars the experienceCalendars to set
     */
    public void setExperienceCalendars(List<String> experienceCalendars) {
        this.experienceCalendars = experienceCalendars;
    }

    /**
     * @param dutyCalendars the dutyCalendars to set
     */
    public void setDutyCalendars(List<String> dutyCalendars) {
        this.dutyCalendars = dutyCalendars;
    }
}
